/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import net.sf.appia.core.message.Message;
import net.sf.appia.protocols.common.RegisterSocketEvent;
import net.sf.appia.protocols.utils.HostUtils;

/**
 *
 * @author Welington
 */
public class Constants {

    private static Class current = Constants.class;
//    public static int SOURCE_PORT = 6789;
//    public static int DESTINATION_PORT = 7034;
//    public static InetAddress SOURCE_ADDRESS;
//    public static InetAddress DESTINATION_ADDRESS;
    private static Debug debug = Debug.getInstance();
    public static final String DEFAULT_MESSAGE_VALUE = "* NO VALUE *";
    public static final String PROCESSES_FILE = "processes.txt";
    public static final int DEFAULT_REQUEST_ID = -1;

    public static final int PROCESS_LABEL = 0;
    public static final int PROCESS_HOST = 1;
    public static final int PROCESS_PORT = 2;
    public static final int PROCESS_IS_CURRENT = 3;
    
    public static Process SOURCE_PROCESS;

//    public static SocketAddress getSourceAddress() {
//        SocketAddress socketAddress = null;
//
//        if (SOURCE_ADDRESS == null) {
//            SOURCE_ADDRESS = HostUtils.getLocalAddress();
//        }
//
//        Constants.SOURCE_PORT = RegisterSocketEvent.FIRST_AVAILABLE;
//        socketAddress = new InetSocketAddress(SOURCE_ADDRESS, Constants.SOURCE_PORT);
//
//        return socketAddress;
//    }
//
//    public static SocketAddress getDestinationAddress() {
//        SocketAddress socketAddress = null;
//        try {
//            if (DESTINATION_ADDRESS == null) {
//                DESTINATION_ADDRESS = HostUtils.getLocalAddress();
//            }
//
//            socketAddress = new InetSocketAddress(DESTINATION_ADDRESS, Constants.DESTINATION_PORT);
//        } catch (Exception ex) {
//            debug.print(Debug.ERROR, current, ex.getLocalizedMessage());
//        }
//
//        return socketAddress;
//    }

    public static String getClassNameFormat() {
        return "%-20s";
    }

    public static String getHourFormat() {
        return "%02d:%02d:%02d:%03d";
    }

    public static String getLabelFormat() {
        return "[%4s]";
    }

    public static Message buildMessage(String content) {
        return new Message(content.getBytes(), 0, content.length());
    }

    public static String extractContent(Message m) {
        if (m != null && m.length() > 0) {
            return new String(m.toByteArray());
        }

        return "";
    }

    public static ArrayList<Process> readFromFile() {

        ArrayList<Process> processes = new ArrayList<Process>();
        String[] lines = FileHandler.load(PROCESSES_FILE);

        if (lines == null) {
            debug.println(Debug.ERROR, "File not found");
        } else {
            int pid = 0;
            for (String line : lines) {
                String[] parts = line.split("\\s");

                if (parts.length < 3) {
                    debug.println(Debug.ERROR, "Wrong process description format in line " + (pid + 1) + ". It was expected (ID LABEL HOST PORT [CURRENT]).");
                } else {
                    Process p = new Process();
                    p.setId(pid);
                    fillProcess(p, parts);
                    

                    processes.add(p);
                }
                
                pid++;
            }

            if (SOURCE_PROCESS == null)
                debug.println(Debug.ERROR, "Process source was not found in the file");
        }

        return processes;
    }

    private static void fillProcess(Process p, String[] parts) throws NumberFormatException {
        p.setLabel(parts[PROCESS_LABEL]);
        p.setHost(parts[PROCESS_HOST]);
        p.setPort(Integer.parseInt(parts[PROCESS_PORT]));

        if (parts.length > 3 && SOURCE_PROCESS == null) {
            SOURCE_PROCESS = p;
        }
    }
}
